import React,{Component} from 'react'
import {getObject} from '../../config/public'
import Pages from '../Pages'
import {Link} from 'react-router'
import * as pub from '../../config/public';
import * as fetch from '../../config/fetch';
import {message,Spin,Button} from 'antd';
import Layer from '../Layer';
class CourseManage extends Component {
	constructor(args) {
		super()
		this.state = {
			questionList : getObject(0),
			loading:false,
			totalPage:1,
			jumpPage:1,
			testpaperTitle:'',
			deleteCourseId:'',
			showLayer:false
		}
			
	}
	componentWillMount() {
		this.show();
	}
	jump(page) {
		this.setState({
			jumpPage: page
		},()=>{
			this.show();
		})
	}
	show() {
		pub.loading(this, true);
		let body = {};
		body.testpaperTitle = this.state.testpaperTitle;
		body.currentPage = this.state.jumpPage;
		fetch.questionManage(body).then((data) => {
			if (data.result === 'RC100') {
				this.setState({
					questionList: data.testPapers.pageItems,
					totalPage: data.testPapers.totalPage
				})
			} else {
				this.setState({
					questionList: [],
				})
				message.error(data.errMsg, 3);
			}

			pub.loading(this, false);
		}, (res) => {
			pub.loading(this, false);
			pub.reject(res);
		})
	}
	// del(id) {
	// 	fetch.delQuestion({testpaperId: id}).then((data) => {
	// 		if (data.result === 'RC100') {
	// 			this.show();
	// 		} else {
	// 			message.error(data.errMsg, 3);
	// 		}
	// 	}, (res) => {
	// 		pub.loading(this, false);
	// 		pub.reject(res);
	// 	})
	// }
	deleteManCourse(testpaperId,opertionState){
		pub.loading(this,true);
		fetch.deleteTestPaper({testpaperId,opertionState}).then((data) => {
			if (data.result === 'RC100') {
				this.show();
				this.setState({
				showLayer:false,
				deleteCourseId:''
		       })
			} else {
				message.error(data.errMsg, 3);
			    this.setState({
				showLayer:false,
				deleteCourseId:''
		       })
			}
			pub.loading(this,false);
		}, (res) => {
			pub.reject(res);
			this.setState({
				showLayer:false,
				deleteCourseId:''
		       })
		})
		pub.loading(this,false);
	}
	deleteCourseId(id){
		this.setState({
			showLayer:true,
			deleteCourseId:id
		})
	}
	deleteCourseIdhideModel(){
		this.setState({
			showLayer:false
		})
	}
	render() {

		return (
			<div className="container">
				<div className="common-title">综合考题管理
					<div className="filter-search floatR">
						<input type="text" value={this.state.testpaperTitle} onChange={(e)=>{this.setState({testpaperTitle:e.target.value.replace(/[@#%&{}?/><[()（）【】{}《》'“‘”’]+/g,'')})}} placeholder="考题搜索"/>
						<a className="btn search-btn" onClick={()=>this.show()}><i className="icon-search"></i></a>
					</div>
				</div>
				<div className="row btn-filter">
					<Link to='/admin/AddQuestion' className="btn btn-blue">考题生成</Link>
				</div>
      
                <Spin spinning={this.state.loading} tip="加载中...">
				<div className="row pad-t-20">
					<div className="questions-table bg-FFFFFF box-shadow" style={{minHeight:'506px'}}>
						<table>
							<thead>
								<tr>
									<th>考题名称</th>
									<th>考题地址</th>
									<th>题目数量</th>
									<th>生成时间</th>
									<th>考试时间</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
							{this.state.questionList.map((question,index)=>{
								return(
								<tr key={index}>
									<td style={{width:'15%'}}>{question.testpaperTitle}</td>
									<td>{window.location.host+'/%23/TestPaper/'+question.testpaperId}</td>
									<td>{question.titleSum}</td>
									<td>{pub.formatTimestamp(question.createTime)}</td>
									<td>{pub.formatTimestamp(question.examStartTime,'y-m-d')}--{pub.formatTimestamp(question.examEndTime,'y-m-d')}</td>
									<td>
										<Link to={'/admin/QuestionDetail/' + question.testpaperId } className="btn btn-blue" style={{marginLeft: '3px'}}>查看</Link>
										{question.opertionState==='U'&&(JSON.parse(localStorage.getItem('user')).manageCom).length===1?<a className="btn btn-blue" style={{marginLeft: '3px'}} onClick={()=>this.deleteManCourse(question.testpaperId,'A')}>上架</a>:null}
										{question.isFinish===1&&question.opertionState==='A'&&(JSON.parse(localStorage.getItem('user')).manageCom).length===1?<a className="btn btn-red" style={{marginLeft: '3px'}} onClick={()=>this.deleteManCourse(question.testpaperId,'U')}>下架</a>:null}
										{(JSON.parse(localStorage.getItem('user')).manageCom).length===1?<a className="btn btn-blue" style={{marginLeft: '3px'}} onClick={()=>this.deleteCourseId(question.testpaperId)}>删除</a>:null}
										{question.isFinish === 0 ? <Link to={'/admin/EditQuestion/' + question.testpaperId} className="btn btn-blue" style={{ marginLeft: '3px' }}>编辑</Link> : null}
									</td>
								</tr>
									)
							})}
							</tbody>
						</table>
					</div>
					<Layer 
						title = ""
						show={this.state.showLayer} 
						hide={this.deleteCourseIdhideModel.bind(this)}
						width="300px"
						height="150px"
					>
					    <div className="row">
							<div className="pop-box-1">																					
								<p>你确定要删除吗？确定后将不能恢复！</p>										
							</div>
									<div className="pop-btn" style={{padding:'7px 0'}}>
										<Button loading={this.state.loading} onClick={()=>this.deleteCourseIdhideModel()} className="btn btn-blue">取消</Button>
										<Button loading={this.state.loading} className="btn btn-blue" onClick={()=>{this.deleteManCourse(this.state.deleteCourseId,'D')}}>确认</Button>
									</div>
						</div>
			        </Layer>
					<div className="line"></div>
					<Pages totalPage={this.state.totalPage} jump={this.jump.bind(this)} />
			   </div>
			   </Spin>

			 

			</div>

		)
	}

}
export default CourseManage

